﻿@{
    Layout = null;
}
@Styles.Render("~/Content/dhtmlxTree/css")
@Scripts.Render("~/Content/dhtmlxTree/js")
<style>
    .containerTableStyle {
        overflow: auto ! important;
    }
</style>
<script type="text/javascript">

    //初始化树控件
    var myTree;
    //加载树
    $.ajax({
        type: "GET",
        url: "@Html.Raw(Url.Action("GetCategoryInfo", "Admin_User"))",
        data: { "DbCode": $("#dbCode").val(), "catid": $("#categoryType").val(), "dbID": $("#dbID").val() },
        beforeSend: function () { $(".talkDivBgLoadTree").show(); $("#loadingcode").show(); },
        success: function (msg) {
            if (msg != "") {
                myTree = new dhtmlXTreeObject("treeboxbox_tree", "100%", "100%", 0);
                myTree.setImagePath("Content/dhtmlxTree/codebase/imgs/dhxtree_skyblue/");
                myTree.enableThreeStateCheckboxes(true);
                myTree.enableCheckBoxes(1);
                myTree.enableSmartXMLParsing(true);
                myTree.loadXMLString(msg);
                checkCode();
            }
            else {
                messageAlert("没有获取到分类数据！");
            }
            $("#loadingcode").hide();
            $(".talkDivBgLoadTree").hide();
        },
        error: function (e) {
            messageAlert($('#fieldname').val() + "字段不是导航字段，请重新设置！");
            $(".talkDivBgLoadTree").hide();
            $("#loadingcode").hide();
            $("#btnClose").click(); //关闭页面
        }
    })

    //已经设置的勾选
    function checkCode() {
        var code = $("#fieldvalue").val();
        var arrayNodes = code.split(',');
        for (var i = 0; i < arrayNodes.length; i++) {
                myTree.setCheck(arrayNodes[i], true);
        }
    }
    function checkAll() {
        var arrayNodes = myTree.getAllUnchecked();
        if (arrayNodes != "") {
            arrayNodes = arrayNodes.split(',');
            for (var i = 0; i < arrayNodes.length; i++) {
                if (arrayNodes[i] != "0") { //虚拟根节点0不能选中
                    myTree.setCheck(arrayNodes[i], true);
                }
            }
        }
        //--全部选中
    }
    function unCheckAll() {
        var arrayNodes = myTree.getAllChecked();
        if (arrayNodes != "") {
            arrayNodes = arrayNodes.split(',');

            for (var i = 0; i < arrayNodes.length; i++) {
                myTree.setCheck(arrayNodes[i], false);
            }
        }
        //取消选中
    }


    //先卸载再加载事件
    $("#btnSubmit").unbind("click").click(function () {
        var dbCode = $('#dbCode').val();
        var fieldname = $('#fieldname').val();
        var checkstr = myTree.getAllChecked();
        var SelectText = "";
        var SelectValue = "";
        if ((checkstr == "" || checkstr == "0") && myTree.getAllFatItems() != "") {
            $("#display" + $('#fieldname').val()).val(""); //清空内容
            $("#" + $('#fieldname').val()).val(""); //清空内容
        }
        else {
            var datastr = { "dbCode": dbCode, "fieldname": fieldname, "checkstr": checkstr, "type": $("#categoryType").val() };
            var urlstr = timestamp("@Html.Raw(Url.Action("GetNaviInfo", "AddNavi"))");
            $.ajax({
                type: "POST",
                url: urlstr,
                data: datastr,
                success: function (data) {
                    $("#display" + $('#fieldname').val()).val(""); //清空内容
                    $("#" + $('#fieldname').val()).val(""); //清空内容
                    var list = data.item.split(',');
                    $.each(list, function (i, value) {

                        if (SelectValue.length > 0)
                            SelectValue += ",";
                        SelectValue += value;

                        if (SelectText.length > 0)
                            SelectText += ",";
                        SelectText += myTree.getItemText(value);

                    });
                    $("#display" + $('#fieldname').val()).val(SelectText);//给用户显示使用不保存（可以用SelectValue）
                    $("#" + $('#fieldname').val()).val(SelectValue);//隐藏控件插入数据使用保存

                    $("#" + $('#NaviName').val()).val(""); //清空内容
                    $("#" + $('#NaviName').val()).val(SelectText);//插入数据使用保存-web.config中配置
                    $("#" + $('#NaviName').val()).attr({ readonly: 'true' }); //添加readonly属性

                },
                error: function (e)
                { }
            })
        }
        //关闭页面
        $('#btnClose').click();
    })
</script>

<div class="f-gp">
    <div class="f-ct" style="margin-left: 0px">
        <label class="f-label">
            选择分类导航
                    <select class="f-select span3" id="categoryType">
                        @if (ViewBag.CategoryType != null)
                        {
                            foreach (var item in ViewBag.CategoryType)
                            {
                                if (ViewBag.ClsID == item.SysID)
                                {   
                            <option value="@item.SysID"   selected="selected" >@item.Name</option>
                                }

                            }
                        }
                    </select>
            @{
                var ajaxOptions = new AjaxOptions()
                {
                    HttpMethod = "post",
                    UpdateTargetId = "categroyList",
                    InsertionMode = InsertionMode.Replace
                };
                @Ajax.ActionLink("category", "/AddNavi/GetCategroyList", new { type = 1 }, ajaxOptions, new { id = "UpdateCatLink", hidden = "hidden", style = "display:none" })
            }
        </label>

    </div>
</div>

<div class="f-gp">
    <div class="f-ct" style="margin-left: 0px">
        <div class="tr" style="display:none;">
            <a onclick="checkAll()">全选</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onclick="unCheckAll()">清除</a>
        </div>
    </div>
</div>
<div class="f-gp" style="margin-top: 15px">
    <div class="f-ct" style="width: 420px; height: 250px; overflow: auto; margin-left: 0px">
        <div class="talkDivBgLoadTree" style="background-color: #000; display: none; opacity: 0.1; z-index: 999; filter: alpha(opacity=30); width: 100%; height: 400px; position: absolute; top: 65px; left: 0; bottom: 50px;">
            @*树加载时遮罩层*@
        </div>
        <div id="loadingcode" style="display: none; margin-left: 160px; margin-top: 58px; position: absolute">
            <img src="~/Images/loading.gif" width="50" />
        </div>
        <div id="categroyList">
            <div id="treeboxbox_tree" enablecheckboxes="1" enabletreeimages="0" enablemultilineitems="1" enableautotooltips="1" enablethreestatecheckboxes="1"
                setimagepath="~/Content/dhtmlxTree/codebase/imgs/dhxtree_skyblue/" style="height: 240px;">
                <ul id="treeli">
                </ul>
                <!--透明背景-->

            </div>
        </div>
    </div>
</div>

<input type="hidden" id="dbID" name="dbID" value="@ViewBag.dbID" />
<input type="hidden" id="dbCode" name="dbCode" value="@ViewBag.dbCode" />
<input type="hidden" id="dbName" name="dbName" value="@ViewBag.dbName" />
<input type="hidden" id="sysid" name="sysid" value="@ViewBag.sysid" />
<input type="hidden" id="fieldname" name="fieldname" value="@ViewBag.fieldname" />
<input type="hidden" id="NaviName" name="NaviName" value="@ViewBag.NaviName" />
<input type="hidden" id="fieldvalue" name="fieldvalue" value="@ViewBag.fieldvalue" />
<input type="hidden" id="ClsID" name="ClsID" value="@ViewBag.ClsID" />



